package cn.kailangtu.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import cn.kailangtu.myapplication.data.Address;
import cn.kailangtu.myapplication.data.yemian;
import cn.kailangtu.myapplication.databinding.ActivityMainBinding;
import cn.kailangtu.myapplication.events.SerialData;
import cn.kailangtu.myapplication.events.SerialDataRS481;
import cn.kailangtu.myapplication.events.Shuaxing灯Event;
import cn.kailangtu.myapplication.services.SerialDataService;
import cn.kailangtu.myapplication.services.Service_Rs485_1;
import cn.kailangtu.serialport.ReceiveData;

import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;

import android.content.Intent;
import android.util.Log;



public class MainActivity extends AppCompatActivity {

     //队列
    private LinkedBlockingQueue<ReceiveData> reportMsgQueue = new LinkedBlockingQueue<>();
    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestPermissions(99);
        hideNavigate();


        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


//        //只透明状态栏
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
//            return;
//        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
//    }
//




//            //隐藏虚拟按键，并且全屏
//            if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
//                View v = this.getWindow().getDecorView();
//                v.setSystemUiVisibility(View.GONE);
//            } else if (Build.VERSION.SDK_INT >= 19) {
//                //for new api versions.
//                View decorView = getWindow().getDecorView();
//                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
//                decorView.setSystemUiVisibility(uiOptions);
//            }
//
//        getWindow().setStatusBarColor(Color.TRANSPARENT);// Color.TRANSPARENT = 0 表示#00000000即透明颜色
//        getWindow().setNavigationBarColor(Color.TRANSPARENT);
//
//        //隐藏状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//



        //启动后台服务
        Intent intent = new Intent(MainActivity.this, SerialDataService.class);
        startService(intent);
        Intent intentRs485 = new Intent(MainActivity.this, Service_Rs485_1.class);
        startService(intentRs485);

      //  Log.e("wwwwww","11111！！");

//        binding.buttonFirst2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                View decorView = getWindow().getDecorView();
//                // 调用DecorView的setSystemUiVisibility()方法，并传入SYSTEM_UI_FLAG_HIDE_NAVIGATION标志
//              //decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
//
//            }
//
//            }
//        );

        // 开始定时任务
        handler.post(runnable);
    }
    private int counter = 0;
    int t1=0;
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            counter++;
            // 在这里更新UI或进行其他操作
            // 例如：updateUI(counter);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
            String t=format.format(new Date());
            Calendar calendar =Calendar.getInstance();
            int day =calendar.get(Calendar.DAY_OF_WEEK);
            String today =null;
            if(day == 2){
                today="星期一";
            }else if(day==3){
                today="星期二";
            }else if(day==4){
                today="星期三";
            }else if(day==5){
                today="星期四";
            }else if(day==6){
                today="星期五";
            }else if(day==7){
                today="星期六";
            }else if(day==1){
                today="星期日";
            }
           if(t1==1000){t1=1;}
            t1=t1+1;
            Address.getInstance().time=t;
            // 创建并发布事件
            EventBus.getDefault().post(new SerialData(t,t1));
            EventBus.getDefault().post(new SerialDataRS481(t,t1));
           // EventBus.getDefault().post(new SerialData(t1));
            // 重新post自己以实现定时累加    循环时间10MS
            handler.postDelayed(this, 100);
        }
    };



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //EventBus  推出
        EventBus.getDefault().unregister(this);
        // 取消定时任务以防止内存泄漏
        handler.removeCallbacks(runnable);
    }


    // 请求权限
    public void requestPermissions(int requestCode) {
        try {
            if (Build.VERSION.SDK_INT >= 23) {
                ArrayList<String> requestPerssionArr = new ArrayList<>();
                int hasCamrea = checkSelfPermission(android.Manifest.permission.CAMERA);
                if (hasCamrea != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(android.Manifest.permission.CAMERA);
                }

                int hasSdcardRead = checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE);
                if (hasSdcardRead != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
                }

                int hasSdcardWrite = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (hasSdcardWrite != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                }
                // 是否应该显示权限请求
                if (!requestPerssionArr.isEmpty()) {
                    String[] requestArray = new String[requestPerssionArr.size()];
                    for (int i = 0; i < requestArray.length; i++) {
                        requestArray[i] = requestPerssionArr.get(i);
                    }
                    requestPermissions(requestArray, requestCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void hideNavigate(){
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

        getWindow().setStatusBarColor(Color.TRANSPARENT);// Color.TRANSPARENT = 0 表示#00000000即透明颜色
        getWindow().setNavigationBarColor(Color.TRANSPARENT);

        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }


}